//
//  RW_UserSingleModel.m
//  SleepApp
//
//  Created by chuangqi on 2021/3/30.
//  Copyright © 2021 RiceWang. All rights reserved.
//

#import "RW_UserSingleModel.h"

@implementation RW_UserSingleModel
//返回一个 Dict，将 Model 属性名对映射到 JSON 的 Key。
//+ (NSDictionary *)modelCustomPropertyMapper {
//    return @{@"list" : @"user_song_list",};
//}
+ (NSDictionary *)modelContainerPropertyGenericClass {
    return @{
        @"list":RW_UserSongModel.class
    };
}

#pragma mark -- 网络请求
/** 创建用户歌单 */
+ (FBLPromise<NSString *> *)createUserSingleName:(NSString *)singleName desc:(NSString *)singleDesc{
    return [FBLPromise async:^(FBLPromiseFulfillBlock  _Nonnull fulfill, FBLPromiseRejectBlock  _Nonnull reject) {
        [HTTPRequestTool putWithUrl:createUserSingleUrl refreshCache:NO params:@{
            @"name":singleName,
            @"desc":singleDesc
        } success:^(id response) {
            HTTPLogicalModel *model = [HTTPLogicalModel yy_modelWithJSON:response];
            if (!model.success){
                reject([NSError errorWithDomain:model.message code:model.code userInfo:nil]);
                return;
            }
            fulfill(model.data);
        } fail:^(NSError *error) {
            reject(error);
        }];
    }];
}
/** 所有收藏歌单获取 */
+ (FBLPromise<NSArray<RW_UserSingleModel *> *> *)getUserSingleList{
    return [FBLPromise async:^(FBLPromiseFulfillBlock  _Nonnull fulfill, FBLPromiseRejectBlock  _Nonnull reject) {
        [HTTPRequestTool getWithUrl:getUserSingleListUrl refreshCache:NO params:nil success:^(id response) {
            HTTPLogicalModel *model = [HTTPLogicalModel yy_modelWithJSON:response];
            if (!model.success){
                reject([NSError errorWithDomain:model.message code:model.code userInfo:nil]);
                return;
            }
            NSArray<RW_UserSingleModel *> *m = [NSArray yy_modelArrayWithClass:RW_UserSingleModel.class json:model.data];
            fulfill(m);
        } fail:^(NSError *error) {
            reject(error);
        }];
    }];
}
/** 收藏歌单详情 */
+ (FBLPromise<RW_UserSingleModel *> *)getUserSingleSongListId:(NSString *)xid{
    return [FBLPromise async:^(FBLPromiseFulfillBlock  _Nonnull fulfill, FBLPromiseRejectBlock  _Nonnull reject) {
        [HTTPRequestTool getWithUrl:getUserSingleSongListUrl refreshCache:NO params:@{
                @"xid":xid
        } success:^(id response) {
            HTTPLogicalModel *model = [HTTPLogicalModel yy_modelWithJSON:response];
            if (!model.success){
                reject([NSError errorWithDomain:model.message code:model.code userInfo:nil]);
                return;
            }
            RW_UserSingleModel *m = [RW_UserSingleModel yy_modelWithJSON:model.data];
            fulfill(m);
        } fail:^(NSError *error) {
            reject(error);
        }];
    }];
}

/** 添加用户歌单绑定关系 */
+ (FBLPromise<NSString *> *)addSongToSingleParams:(NSDictionary *)params{
    return [FBLPromise async:^(FBLPromiseFulfillBlock  _Nonnull fulfill, FBLPromiseRejectBlock  _Nonnull reject) {
        [HTTPRequestTool putWithUrl:addSongTreeUrl refreshCache:NO params:params success:^(id response) {
            HTTPLogicalModel *model = [HTTPLogicalModel yy_modelWithJSON:response];
            if (!model.success){
                reject([NSError errorWithDomain:model.message code:model.code userInfo:nil]);
                return;
            }
            fulfill(model.data);
        } fail:^(NSError *error) {
            reject(error);
        }];
    }];
}

/** 删除歌单信息 */
+ (FBLPromise<NSString *> *)deleteToUserSingleId:(NSString *)userSingleId{
    return [FBLPromise async:^(FBLPromiseFulfillBlock  _Nonnull fulfill, FBLPromiseRejectBlock  _Nonnull reject) {
        [HTTPRequestTool putWithUrl:deleteUserSingleUrl refreshCache:NO params:@{
            @"xid":userSingleId
        } success:^(id response) {
            HTTPLogicalModel *model = [HTTPLogicalModel yy_modelWithJSON:response];
            if (!model.success){
                reject([NSError errorWithDomain:model.message code:model.code userInfo:nil]);
                return;
            }
            fulfill(model.data);
        } fail:^(NSError *error) {
            reject(error);
        }];
    }];
}


/** 更新歌单名称*/
+ (FBLPromise<NSString *> *)updataUserSingleId:(NSString *)userSingleId name:(NSString *)name{
    return [FBLPromise async:^(FBLPromiseFulfillBlock  _Nonnull fulfill, FBLPromiseRejectBlock  _Nonnull reject) {
        [HTTPRequestTool putWithUrl:updataUserSingleNameUrl refreshCache:NO params:@{
            @"xid":userSingleId,
            @"name":name
        } success:^(id response) {
            HTTPLogicalModel *model = [HTTPLogicalModel yy_modelWithJSON:response];
            if (!model.success){
                reject([NSError errorWithDomain:model.message code:model.code userInfo:nil]);
                return;
            }
            fulfill(model.data);
        } fail:^(NSError *error) {
            reject(error);
        }];
    }];
}




@end
